package com.tsing.wms.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsing.wms.entity.OutInRecordEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tsing.wms.vo.req.RecordPageReqVO;
import com.tsing.wms.vo.resp.RecordRespVO;
import com.tsing.wms.vo.resp.StockListRespVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 出入库记录表 Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2022-04-13
 */
public interface OutInRecordMapper extends BaseMapper<OutInRecordEntity> {

    @Select("select s.id,s.goods_id,s.amount,concat(su.name,' - ',g.name) name,g.specs\n" +
            "from t_stock s\n" +
            "inner join t_goods g on s.goods_id=g.id\n" +
            "inner join t_supplier su on g.supplier_id=su.id\n" +
            "where s.storehouse_id=#{id}")
    List<StockListRespVO> stockList(@Param("id") String id);

    @Select("select rd.goods_id,rd.amount,g.name goodsName,g.specs,g.price from t_record_detail rd\n" +
            "left join t_goods g on rd.goods_id=g.id\n" +
            "where rd.out_record_id=#{id}")
    List<RecordRespVO.RecordDetailRespVO> selectRecordDetails(@Param("id") String id);

    @Select("<script>" +
            "select o.*,s.name storehouseName,u.username userName\n" +
            "from t_out_in_record o\n" +
            "left join t_storehouse s on o.storehouse_id=s.id\n" +
            "left join t_user u on o.user_id=u.id\n" +
            "where o.type=#{reqVO.type}\n" +
            "<if test=\"reqVO.id!=null and reqVO.id!=''\">and o.id like concat('%',#{reqVO.id},'%')</if>" +
            "order by o.id desc" +
            "</script>")
    IPage<RecordRespVO> recordPage(IPage<RecordRespVO> page, @Param("reqVO") RecordPageReqVO reqVO);

    @Select("select sum(amount) num from t_record_detail where out_record_id=#{id}")
    Long selectGoodsCount(@Param("id") String id);

}
